function [ChEstimate,error] = HF_WLSChEstimate(ChOutput,ChInput,ChLength)
% ***************************************************
% 
% INPUTS:
%        ChOutput : The outputs of the channel sampled at one sample per symbol
%        ChInput  : The input symbols to the channel sampled at one sample per symbol 
%        ChLength : Length of composite channel
%
% OUTPUTS:
%       ChEstimate: estimated composite channel, i.e. (KnownCh*UnKnownCh)
%       error2    : square sum of the estimation error.
% ********************************************************
N=length(ChInput)-ChLength+1;

EsNo_test = (10.^(20/10)); 
I_Np = eye(N,N);

W = 1/EsNo_test*I_Np;

for index = 1:ChLength
    H(:,index) = ChInput(ChLength-index+1:end-index+1).';
end

ChEstimate = pinv(H'*W*H)*H'*W*ChOutput(ChLength:end).';


ChOutput_fromestimation = conv(ChInput,ChEstimate);

ChOutput_fromestimation = ChOutput_fromestimation(1:length(ChOutput));

error_vector = ChOutput_fromestimation - ChOutput;

error = mean (abs(error_vector).^2);

[]